package com.jsgt.greekfire.ui.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.fhx.core.base.BaseAdapter
import com.fhx.core.base.BaseViewHolder
import com.jsgt.greekfire.R
import com.jsgt.greekfire.dao.model.MarketBean

class HomeAdapter : BaseAdapter<MarketBean>() {
    private var gftCoinBean: MarketBean? = null


    override fun provideViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder? {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_home_header, parent, false)
        return VH(view)
    }

    inner class VH(itemView: View) : BaseViewHolder(itemView) {
        var tv_1: TextView? = itemView.findViewById(R.id.tv_1)
        var tv_2: TextView? = itemView.findViewById(R.id.tv_2)
        var tv_3: TextView? = itemView.findViewById(R.id.tv_3)


        fun bindData(bean: MarketBean) {

            tv_1?.text = bean.symbol!!.toString().replace("_","/").toUpperCase()
            tv_2?.text = bean.last.toString()

            tv_3?.text = bean.changePercentage.toString()

//            if (bean.changepercentage?.startsWith("-")!!) {
//                tv_3?.text = itemView.resources.getColor(R.color.red_DE6C65).toString()
//            } else {
//                tv_3?.text = bean.changepercentage.toString()
//                tv_3?.text = itemView.resources.getColor(R.color.green_6AB578).toString()
//            }


        }
    }

    override fun getItemCount(): Int {
        return if (gftCoinBean == null) dataList().size else dataList().size + 1
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val viewHolder = holder as VH
        if (gftCoinBean != null) {
            if (position == 0) {
                viewHolder.bindData(gftCoinBean!!)
            } else {
                viewHolder.bindData(dataList()[position - 1])
            }
        } else {
            viewHolder.bindData(dataList()[position])
        }
    }

    override fun setItemListener(viewHolder: BaseViewHolder, viewType: Int) {
        if (!isClickEnabled(viewType)) return
        if (itemClickListner != null) {
            viewHolder.itemView.setOnClickListener {
                if (gftCoinBean == null) {
                    if (dataList().size > viewHolder.adapterPosition && viewHolder.adapterPosition >= 0) {
                        itemClickListner?.invoke(dataList()[viewHolder.adapterPosition])
                    }
                } else {
                    if (viewHolder.adapterPosition == 0) {
                        itemClickListner?.invoke(gftCoinBean!!)
                    } else if (dataList().size + 1 > viewHolder.adapterPosition && viewHolder.adapterPosition >= 0) {
                        itemClickListner?.invoke(dataList()[viewHolder.adapterPosition - 1])
                    }
                }
            }
        }
    }

    fun bindGFT(gftCoinBean: MarketBean?) {
        this.gftCoinBean = gftCoinBean
        notifyDataSetChanged()
    }
}